#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution
{
public:
    long long subArrayRanges(vector<int> &nums)
    {
        long long t_min = 10e9 + 9;
        long long t_max = -t_min;
        long long ans = 0;
        for (int i = 0; i < nums.size() - 1; i++)
        {
            t_min = t_max = nums[i];
            for (int j = i + 1; j < nums.size(); j++)
            {
                t_min = min(t_min, (long long)nums[j]);
                t_max = max(t_max, (long long)nums[j]);
                ans += t_max - t_min;
            }
        }
        return ans;
    }
};
int main()
{
    Solution *s = new Solution();
    vector<int> v{4, -2, -3, 4, 1};
    long long ans = s->subArrayRanges(v);
    cout << ans << endl;
    return 0;
}